package bb.lanxing.model.database;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.garmin.fit.MonitoringReader;
//import com.orm.query.Select;
import com.umeng.analytics.pro.d;

import bb.lanxing.util.JsonUtil;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;
import org.litepal.LitePal;

public class Segment extends LitePalTableBase {
    private static SQLiteDatabase db;
    private double distance;
    private double distanceCount;
    private long duration;
    private long durationCount;
    private long endIndex;
    private long endTime;
    private long startIndex;
    private long startTime;
    private boolean upload;
    private long workoutId;

    public Segment() {
    }

    public Segment(long workoutId) {
        this.workoutId = workoutId;
    }

    public Segment(JSONObject node, Workout workout) {
        setStartTime(JsonUtil.getLongValue("startTime", node));//d.p
        setDuration(JsonUtil.getLongValue("duration", node));
        setDistance(JsonUtil.getLongValue(MonitoringReader.DISTANCE_STRING, node));
        setEndIndex(JsonUtil.getLongValue("endIndex", node));
        setStartIndex(JsonUtil.getLongValue("startIndex", node));
        setEndTime(JsonUtil.getLongValue("endTime", node));//d.q
        setUpload(true);
        setWorkoutId(workout.getId());
        setDistanceCount(workout.getDistance());
        setDurationCount(workout.getDuration());
    }

    public static JSONObject toJson(Segment segment) throws JSONException {
        JSONObject jSONObject = new JSONObject();
        jSONObject.put("startIndex", segment.getStartIndex());
        jSONObject.put("endIndex", segment.getEndIndex());
        jSONObject.put("startTime", segment.getStartTime());
        jSONObject.put("endTime", segment.getEndTime());
        jSONObject.put("distance", segment.getDistance());
        jSONObject.put("duration", segment.getDuration());
        return jSONObject;
    }

    public static Segment createSegment(Workout workout) {
        if (workout == null) {
            return null;
        }
        long longValue = workout.getId();
        long countByWorkout = Trackpoint.getCountByWorkout(longValue);
        Segment lastByWorkoutId = getLastByWorkoutId(longValue);
        Segment segment = new Segment(longValue);
        if (lastByWorkoutId == null) {
            segment.setDistance(workout.getDistance());
            segment.setDuration(workout.getDuration());
            segment.setStartIndex(0L);
            segment.setStartTime(workout.getStartTime());
        } else if (lastByWorkoutId.getEndIndex() >= countByWorkout - 1) {
            return null;
        } else {
            segment.setDistance(workout.getDistance() - lastByWorkoutId.getDistanceCount());
            segment.setDuration(workout.getDuration() - lastByWorkoutId.getDurationCount());
            segment.setStartIndex(lastByWorkoutId.endIndex + 1);
            segment.setStartTime(lastByWorkoutId.endTime);
        }
        segment.setDistanceCount(workout.getDistance());
        segment.setDurationCount(workout.getDuration());
        segment.setEndIndex(countByWorkout - 1);
        segment.setEndTime(workout.getEndTime());
        segment.setUpload(false);
        segment.save();
        return segment;
    }

    public static void deleteByWorkout(long workoutId) {
        String id = String.valueOf(workoutId);
        LitePal.deleteAll(Segment.class, "workoutId = ?", id);
    }

    public static long getUploadedCount(long workoutId) {
        SQLiteDatabase db2 = LitePal.getDatabase();
        db = db2;
        Cursor rawQuery = db2.rawQuery("SELECT max(endindex) FROM segment WHERE workoutid = ? and upload = 1", new String[]{String.valueOf(workoutId)});
        long j = (!rawQuery.moveToNext() || rawQuery.isNull(0)) ? 0L : rawQuery.getLong(0) + 1;
        rawQuery.close();
        return j;
    }

    public static Segment getLastByWorkoutId(long workoutId) {
        List<Segment> byWorkoutId = getByWorkoutId(workoutId);
        if (byWorkoutId.size() > 0) {
            return byWorkoutId.get(byWorkoutId.size() - 1);
        }
        return null;
    }

    public static List<Segment> getByWorkoutId(long workoutId) {
        return LitePal.where("workoutId =? ", String.valueOf(workoutId)).find(Segment.class);
    }

    public static List<Segment> getUnupload(long workoutId) {

        List<Segment> l = LitePal.where("workoutId = ? and upload = 0", String.valueOf(workoutId)).find(Segment.class);
        return l;

//        return Select.from(Segment.class).where("workoutId = ? and upload = 0", new String[]{String.valueOf(workoutId)}).list();
    }

    public static long getLastTime(long workoutId) {
        SQLiteDatabase db2 = LitePal.getDatabase();
        db = db2;
        Cursor rawQuery = db2.rawQuery("SELECT max(endtime) FROM segment WHERE workoutid = ?", new String[]{String.valueOf(workoutId)});
        long j = rawQuery.moveToNext() ? rawQuery.getLong(0) : 0L;
        rawQuery.close();
        return j;
    }

    public boolean isUpload() {
        return this.upload;
    }

    public void setUpload(boolean upload) {
        this.upload = upload;
    }

    public long getWorkoutId() {
        return this.workoutId;
    }

    public void setWorkoutId(long workoutId) {
        this.workoutId = workoutId;
    }

    public long getStartIndex() {
        return this.startIndex;
    }

    public void setStartIndex(long startIndex) {
        this.startIndex = startIndex;
    }

    public long getEndIndex() {
        return this.endIndex;
    }

    public void setEndIndex(long endIndex) {
        this.endIndex = endIndex;
    }

    public long getStartTime() {
        return this.startTime;
    }

    public void setStartTime(long startTime) {
        this.startTime = startTime;
    }

    public long getEndTime() {
        return this.endTime;
    }

    public void setEndTime(long endTime) {
        this.endTime = endTime;
    }

    public double getDistance() {
        return this.distance;
    }

    public void setDistance(double distance) {
        this.distance = distance;
    }

    public long getDuration() {
        return this.duration;
    }

    public void setDuration(long duration) {
        this.duration = duration;
    }

    public double getDistanceCount() {
        return this.distanceCount;
    }

    public void setDistanceCount(double distanceCount) {
        this.distanceCount = distanceCount;
    }

    public long getDurationCount() {
        return this.durationCount;
    }

    public void setDurationCount(long durationCount) {
        this.durationCount = durationCount;
    }
}